#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/22 10:51
# @USER    : Shengji He
# @File    : PatternMatchingLCCI.py
# @Software: PyCharm
# @Version  : Python-
# @TASK:
from collections import Counter


class Solution:
    def patternMatching(self, pattern: str, value: str) -> bool:
        """
        You are given two strings, pattern and value. The pattern string consists of just the letters a and b,
        describing a pattern within a string. For example, the string catcatgocatgo matches the pattern aabab
        (where cat is a and go is b). It also matches patterns like a, ab, and b. Write a method to determine
        if value matches pattern. a and b cannot be the same string.

        Example 1:
            Input:  pattern = "abba", value = "dogcatcatdog"

            Output:  true
        Example 2:
            Input:  pattern = "abba", value = "dogcatcatfish"

            Output:  false
        Example 3:
            Input:  pattern = "aaaa", value = "dogcatcatdog"

            Output:  false
        Example 4:
            Input:  pattern = "abba", value = "dogdogdogdog"

            Output:  true

            Explanation:  "a"="dogdog",b=""，vice versa.
        Note:
            - 0 <= len(pattern) <= 1000
            - 0 <= len(value) <= 1000
            - pattern only contains "a" and "b", value only contains lowercase letters.

        :param pattern:
        :param value:
        :return:
        """
        pdict = Counter(pattern)
        if pdict['a'] == 0 and pdict['b'] == 0:
            if len(value) == 0:
                return True
            else:
                return False
        elif pdict['a'] == 0 or pdict['b'] == 0:
            if len(value) % max(pdict['a'], pdict['b']) != 0:
                return False
            else:
                if value[0: int(len(value) / max(pdict['a'], pdict['b']))] * max(pdict['a'], pdict['b']) == value:
                    return True
                else:
                    return False
        else:
            na = 0
            while True:
                if (len(value) - pdict['a'] * na) % pdict['b'] == 0:
                    nb = int((len(value) - pdict['a'] * na) / pdict['b'])
                    idx = 0
                    output = ''
                    pv = {}
                    for element in pattern:
                        if element == 'a':
                            if element in pv:
                                output += pv[element]
                            else:
                                output += value[idx: idx + na]
                                pv[element] = value[idx: idx + na]
                            idx += na
                        else:
                            if element in pv:
                                output += pv[element]
                            else:
                                output += value[idx: idx + nb]
                                pv[element] = value[idx: idx + nb]
                            idx += nb
                    if output == value and pv['a'] != pv['b']:
                        return True
                na += 1
                if na * pdict['a'] > len(value):
                    return False


if __name__ == '__main__':
    S = Solution()
    pattern = "abba"
    value = "dogcatcatdog"

    # pattern = "abba"
    # value = "dogcatcatfish"

    # pattern = "aaaa"
    # value = "dogcatcatdog"

    # pattern = "abba"
    # value = "dogdogdogdog"

    # pattern = 'ab'
    # value = ''

    # pattern = "aaaaab"
    # value = "xahnxdxyaahnxdxyaahnxdxyaahnxdxyaauxuhuo"
    print(S.patternMatching(pattern, value))
    print('done')
